﻿Imports System
Imports System.Drawing

Namespace System.Workflow.ComponentModel.Design
    Public Class SequentialWorkflowHeaderFooter
        ' Methods
        Public Sub New(ByVal parent As SequentialWorkflowRootDesigner, ByVal isHeader As Boolean)
            Me.rootDesigner = parent
            Me.isHeader = isHeader
        End Sub

        Public Overridable Sub OnLayout(ByVal e As ActivityDesignerLayoutEventArgs)
            If (e Is Nothing) Then
                Throw New ArgumentNullException("e")
            End If
            If ((Not String.IsNullOrEmpty(Me.Text) AndAlso (Not e.DesignerTheme Is Nothing)) AndAlso (Not e.DesignerTheme.Font Is Nothing)) Then
                [Using] font As Font = New Font(e.DesignerTheme.Font.FontFamily, (e.DesignerTheme.Font.SizeInPoints + 1!), FontStyle.Bold)
                    Me.textSize = ActivityDesignerPaint.MeasureString(e.Graphics, font, Me.Text, StringAlignment.Center, Size.Empty)
                End [Using]
            End If
        End Sub

        Public Overridable Sub OnPaint(ByVal e As ActivityDesignerPaintEventArgs)
            If (e Is Nothing) Then
                Throw New ArgumentNullException("e")
            End If
            If ((Not String.IsNullOrEmpty(Me.Text) AndAlso Not Me.TextRectangle.Size.IsEmpty) AndAlso ((Not e.DesignerTheme Is Nothing) AndAlso (Not e.DesignerTheme.Font Is Nothing))) Then
                [Using] font As Font = New Font(e.DesignerTheme.Font.FontFamily, (e.DesignerTheme.Font.SizeInPoints + 1!), If(Me.AssociatedDesigner.SmartTagVisible, FontStyle.Bold, FontStyle.Regular))
                    ActivityDesignerPaint.DrawText(e.Graphics, font, Me.Text, Me.TextRectangle, StringAlignment.Center, TextQuality.AntiAliased, e.DesignerTheme.ForegroundBrush)
                End [Using]
            End If
            If ((Not Me.Image Is Nothing) AndAlso Not Me.ImageRectangle.Size.IsEmpty) Then
                ActivityDesignerPaint.DrawImage(e.Graphics, Me.Image, Me.ImageRectangle, DesignerContentAlignment.Fill)
            End If
        End Sub


        ' Properties
        Protected ReadOnly Property AssociatedDesigner As SequentialWorkflowRootDesigner
            Get
                Return Me.rootDesigner
            End Get
        End Property

        Public Overridable ReadOnly Property Bounds As Rectangle
            Get
                Dim empty As Rectangle = Rectangle.Empty
                Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                Dim textRectangle As Rectangle = Me.TextRectangle
                Dim imageRectangle As Rectangle = Me.ImageRectangle
                If (Not textRectangle.Size.IsEmpty OrElse Not imageRectangle.Size.IsEmpty) Then
                    empty.Width = (Math.Max(imageRectangle.Width, textRectangle.Width) + (2 * margin.Width))
                    empty.Height = (margin.Height + imageRectangle.Height)
                    empty.Height = (empty.Height + If((imageRectangle.Height > 0), margin.Height, 0))
                    empty.Height = (empty.Height + textRectangle.Height)
                    empty.Height = (empty.Height + If((textRectangle.Height > 0), margin.Height, 0))
                    Dim bounds As Rectangle = Me.rootDesigner.Bounds
                    empty.X = ((bounds.Left + (bounds.Width / 2)) - (empty.Width / 2))
                    empty.Y = If(Me.isHeader, bounds.Top, (bounds.Bottom - empty.Height))
                End If
                Return empty
            End Get
        End Property

        Public Overridable Property Image As Image
            Get
                Return Me.image
            End Get
            Set(ByVal value As Image)
                If (Not Me.image Is value) Then
                    Me.image = value
                    Me.AssociatedDesigner.InternalPerformLayout
                End If
            End Set
        End Property

        Public Overridable ReadOnly Property ImageRectangle As Rectangle
            Get
                Dim empty As Rectangle = Rectangle.Empty
                If (Not Me.Image Is Nothing) Then
                    Dim designerTheme As ActivityDesignerTheme = Me.rootDesigner.DesignerTheme
                    Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                    Dim bounds As Rectangle = Me.rootDesigner.Bounds
                    Dim textRectangle As Rectangle = Me.TextRectangle
                    empty.Size = designerTheme.ImageSize
                    empty.X = ((bounds.Left + (bounds.Width / 2)) - (empty.Width / 2))
                    If Me.isHeader Then
                        empty.Y = (bounds.Top + margin.Height)
                        empty.Y = (empty.Y + textRectangle.Height)
                        empty.Y = (empty.Y + If((textRectangle.Height > 0), margin.Height, 0))
                        Return empty
                    End If
                    empty.Y = (bounds.Bottom - margin.Height)
                    empty.Y = (empty.Y - textRectangle.Height)
                    empty.Y = (empty.Y - If((textRectangle.Height > 0), margin.Height, 0))
                    empty.Y = (empty.Y - empty.Height)
                End If
                Return empty
            End Get
        End Property

        Public Overridable Property [Text] As String
            Get
                Return Me.text
            End Get
            Set(ByVal value As String)
                If (Me.text <> value) Then
                    Me.text = value
                    Me.AssociatedDesigner.InternalPerformLayout
                End If
            End Set
        End Property

        Public Overridable ReadOnly Property TextRectangle As Rectangle
            Get
                Dim empty As Rectangle = Rectangle.Empty
                If Not String.IsNullOrEmpty(Me.Text) Then
                    Dim margin As Size = WorkflowTheme.CurrentTheme.AmbientTheme.Margin
                    Dim bounds As Rectangle = Me.rootDesigner.Bounds
                    empty.Size = Me.textSize
                    empty.X = ((bounds.Left + (bounds.Width / 2)) - (Me.textSize.Width / 2))
                    empty.Y = If(Me.isHeader, (bounds.Top + margin.Height), ((bounds.Bottom - margin.Height) - Me.textSize.Height))
                End If
                Return empty
            End Get
        End Property


        ' Fields
        Private image As Image
        Private isHeader As Boolean = True
        Private rootDesigner As SequentialWorkflowRootDesigner
        Private [text] As String = String.Empty
        Friend textSize As Size = Size.Empty
    End Class
End Namespace

